﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Helpers;
using System.Linq;
using DevExpress.XtraNavBar;
using DevExpress.XtraTab;
using DevExpress.XtraTabbedMdi;
using System.Configuration;

namespace ProductManager
{
    public partial class ProductManager : DevExpress.XtraBars.Ribbon.RibbonForm
    {

        private ProductMDDataContext ctx = null;
        private gui.GUIProduct product = null;
        public ProductManager()
        {
            InitializeComponent();

            

        }

        

        private void loadDataToListbox() {
            navBarCategory.ItemLinks.Clear();
            navBarCompany.ItemLinks.Clear();

            var category = ctx.Categories.Where(c => c.Status.Equals(true));            
            foreach( Category c  in category){
                NavBarItem items = new NavBarItem(c.CategoryName);
                items.SmallImageIndex = 0;
                items.LinkClicked += new NavBarLinkEventHandler(NavBarItem_Category_LinkClicked);
                navBarCategory.ItemLinks.Add(items);
            }

            var company = ctx.Companies.Where(c => c.Status.Equals(true));
            foreach (Company c in company)
            {
                NavBarItem items = new NavBarItem(c.CompanyName);
                items.SmallImageIndex = 1;
                items.LinkClicked += new NavBarLinkEventHandler(NavBarItem_Company_LinkClicked);
                navBarCompany.ItemLinks.Add(items);
            }
        }


        private void NavBarItem_Category_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            int index = checkMDIChild("Product");
            if (index != -1)
            {
                product.RefreshData(e.Link.Caption, 1);
                this.MdiChildren.ElementAt(index).Activate();
            }
            else
            {
                product = new gui.GUIProduct(e.Link.Caption, 1);
                product.Text = "Product";
                product.MdiParent = this;
                product.Show();
            }
        }

        private void NavBarItem_Company_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            int index = checkMDIChild("Product");
            if (index != -1)
            {
                product.RefreshData(e.Link.Caption, 2);
                this.MdiChildren.ElementAt(index).Activate();
            }
            else
            {
                product = new gui.GUIProduct(e.Link.Caption, 2);
                product.Text = "Product";
                product.MdiParent = this;
                product.Show();
            }
        }

        void InitSkinGallery()
        {
            SkinHelper.InitSkinGallery(ribbonGalleryBarSkin, true);
        }

        private void barbtnExit_ItemClick(object sender, ItemClickEventArgs e)
        {
            DialogResult dlResult = MessageBox.Show(this,"Do you want to exit?","Exit",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (dlResult == DialogResult.Yes) {
                this.Close();
            }
        }

        

        private void barbtnProduct_ItemClick(object sender, ItemClickEventArgs e)
        {
            int index = checkMDIChild("Product");
            
            if (index == -1)
            {
                product = new gui.GUIProduct();
                product.Text = "Product";
                product.MdiParent = this;
                product.Show();
            }
            else
            {
                this.MdiChildren.ElementAt(index).Activate();
            }
        }

        
        private void barbtnCategory_ItemClick(object sender, ItemClickEventArgs e)
        {

            int index = checkMDIChild("Category");
            if (index == -1)
            {
                gui.GUICategory category = new gui.GUICategory();
                category.Text = "Category";
                category.MdiParent = this;
                category.btnAdd.Click += new EventHandler(btnUpdateData_Click);
                category.btnUpdate.Click += new EventHandler(btnUpdateData_Click);
                category.btnDelete.Click += new EventHandler(btnUpdateData_Click);
                category.Show();
            }
            else {
                this.MdiChildren.ElementAt(index).Activate();
            }
        }

        private void btnUpdateData_Click(object sender, EventArgs e) {
            loadDataToListbox();
            if(product !=null){
                product.RefreshData();
            }
        }

        private int checkMDIChild(string caption)
        {
            
            int index = -1;
            for (int i = 0; i < this.MdiChildren.Count(); i++)
            {
                if (this.MdiChildren.ElementAt(i).Text.Equals(caption))
                {
                    index = i;
                    break;
                }
            }

            return index;
        }

        private void barbtnCompany_ItemClick(object sender, ItemClickEventArgs e)
        {
            int index = checkMDIChild("Company");
            if (index == -1)
            {
                gui.GUICompany company = new gui.GUICompany();
                company.Text = "Company";
                company.MdiParent = this;
                company.btnAdd.Click += new EventHandler(btnUpdateData_Click);
                company.btnUpdate.Click += new EventHandler(btnUpdateData_Click);
                company.btnDelete.Click += new EventHandler(btnUpdateData_Click);
                company.Show();
            }
            else
            {
                this.MdiChildren.ElementAt(index).Activate();
            }
        }

        private void barbtnAbout_ItemClick(object sender, ItemClickEventArgs e)
        {
            gui.GUIAbout about = new gui.GUIAbout();
            about.ShowDialog();
        }

        private void barbtnReport_ItemClick(object sender, ItemClickEventArgs e)
        {
            gui.GUIReport report = new gui.GUIReport();
            report.ShowDialog();
        }

        private void barbtnConfig_ItemClick(object sender, ItemClickEventArgs e)
        {
            gui.GUIConfig config = new gui.GUIConfig();
            config.ShowDialog();
        }

        private void ProductManager_Load(object sender, EventArgs e)
        {
            
            
            ctx = new ProductMDDataContext();
            loadDataToListbox();
            InitSkinGallery();

            product = new gui.GUIProduct();
            product.Text = "Product";
            product.MdiParent = this;
            product.Show();
        }

        
    }
}